﻿using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Data.Sql;
using System.Data.SqlClient;
using System.Text;
using System.Threading;
using System.Web;
using System.Web.Security; 
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class welcome : System.Web.UI.Page
{
    protected override void OnInit(EventArgs e)
    {
        Response.Cache.SetCacheability(HttpCacheability.NoCache);
        Response.Cache.SetNoStore();
        Response.Cache.SetExpires(DateTime.MinValue);

        base.OnInit(e);
    }

    protected override void InitializeCulture()
    {
        if (Session["SELECTED_LANGUAGE"] != null)
        {
            string current_language = Session["SELECTED_LANGUAGE"].ToString();

            Thread.CurrentThread.CurrentCulture = CultureInfo.CreateSpecificCulture(current_language);
            Thread.CurrentThread.CurrentUICulture = new CultureInfo(current_language);
        }
        else
        {
            Thread.CurrentThread.CurrentCulture = CultureInfo.CurrentCulture;
            Thread.CurrentThread.CurrentUICulture = CultureInfo.CurrentUICulture;
        }

        base.InitializeCulture();
    }

    protected void Page_Load(object sender, EventArgs e)
    {
        if (Context.User.Identity.IsAuthenticated)
        {
            recreate_panel();
        }       
    }

    protected void btnLogin_Click(object sender, EventArgs e)
    {               
        try
        {
            cmpUser new_user = new cmpUser();

            if (!(Membership.ValidateUser(loginBox.Text, PasswordBox.Text)))
            {
                throw new Exception(GetLocalResourceObject("INCORRECT_LOGIN_NAME_OR_PASSWORD").ToString());
            }
            else
            {
                recreate_panel();
                FormsAuthentication.RedirectFromLoginPage(loginBox.Text, CheckBox1.Checked);
            }
        }
        catch(Exception E)
        {
           Exception_Message_Label.Text = E.Message;
           Deimos.Logging.eventLog.insert_log_event("welcome.aspx.cs", E.Message);
        }
    }

    protected void btnSearch_Click(object sender, EventArgs e)
    {

        StringBuilder sb = new StringBuilder();

        if (TextBox10.Text == "")
        {
            sb.Append("Input search criterias");
        }
        else
        {
            dbProcessor search_db = new dbProcessor();

            SqlDataReader search_result = search_db.search_in_keywords(TextBox10.Text);

            sb.Append("<TABLE border=1>");

            sb.Append("<TR>");

            sb.Append("<TD> Name </TD>");
            sb.Append("<TD> Address </TD>");

            sb.Append("</TR>");

            while (search_result.Read())
            {
                sb.Append("<TR>");

                sb.Append("<TD>" + (string)search_result["Name"] + "</TD>");

                sb.Append("<TD>" + (string)search_result["Address"] + "</TD>");

                sb.Append("</TR>");
            }

            sb.Append("</TABLE>");

        }

        Literal1.Text = sb.ToString();

    }

    protected void btnLogout_Click(object sender, EventArgs e)
    {
        FormsAuthentication.SignOut();
        Response.Redirect("Default.aspx");
    }

    public void recreate_panel()
    {
        loginBox.Visible = false;
        PasswordBox.Visible = false;
        CheckBox1.Visible = false;

        HyperLink1.Visible = false;
        HyperLink2.Visible = false;

        Label2.Visible = false;
        Label3.Visible = false;

        btnLogin.Visible = false;

        Label2.Text = "Logged in as: " + Context.User.Identity.Name;
        Label2.Visible = true;

        LinkButton1.Visible = true;
        LinkButton1.PostBackUrl = "Default.aspx";

        logoutButton.Visible = true;       
    }

    protected void Link_EN_Click(object sender, EventArgs e)
    {
        Session.Add("SELECTED_LANGUAGE", "en-US");
        Response.Redirect(Request.Url.AbsoluteUri);
    }
 
    protected void Link_RU_Click(object sender, EventArgs e)
    {
        Session.Add("SELECTED_LANGUAGE", "ru-RU");
        Response.Redirect(Request.Url.AbsoluteUri);
    }


    protected void LinkButton2_Click(object sender, EventArgs e)
    {
        
    }
}